// localSoftwareInfo.js
const fs = require('fs');
const path = require('path');
const { execSync } = require('child_process');

class LocalSoftwareInfo {
    constructor() {
        // Separate commands for version and path to ensure accuracy
        this.commands = {
            condaVersion: 'conda --version',
            condaPath: 'conda info --base',
            nodeVersion: 'node -v',
            nodePath: 'node -e "console.log(process.execPath)"',
            pythonSystemVersion: process.platform === 'win32' ? 'where python' : 'which python',
            pythonSystemPath: 'python -c "import sys; print(sys.executable)"'
        };
    }

    getSoftwareInfo() {
        const softwareInfo = {};
        const originalPath = process.env.PATH;

        // Handle Python to avoid Conda interference
        softwareInfo.python = this.getSystemPythonInfo(originalPath);

        // Get Conda and Node information
        ['conda', 'node'].forEach((name) => {
            try {
                const versionOutput = execSync(this.commands[`${name}Version`], { stdio: 'pipe' }).toString().trim();
                const installPathOutput = execSync(this.commands[`${name}Path`], { stdio: 'pipe' }).toString().trim();
                const version = this.extractVersion(name, versionOutput);

                softwareInfo[name] = { version, directory: installPathOutput };
            } catch (error) {
                console.error(`Error getting ${name} info`);
                softwareInfo[name] = null;
            }
        });

        // Get labelapp information
        let labelappPath = path.join(__dirname, '..', 'VERSION');
        let labelappVersion = fs.readFileSync(labelappPath, 'utf-8');
        softwareInfo.labelapp = {
            version: labelappVersion,
            directory: labelappPath
        };

        return softwareInfo;
    }

    getSystemPythonInfo(originalPath) {
        const strippedPath = originalPath
            .split(require('path').delimiter)
            .filter((path) => !path.includes('conda'))
            .join(require('path').delimiter);

        try {
            // Temporarily remove Conda from PATH
            process.env.PATH = strippedPath;

            const installPathOutput = execSync(this.commands.pythonSystemPath, { stdio: 'pipe' }).toString().trim();
            const versionOutput = execSync('python --version', { stdio: 'pipe' }).toString().trim();
            const version = this.extractVersion('python', versionOutput);

            return { version, directory: installPathOutput };
        } catch (error) {
            // console.error('Error getting system Python info');
            return null;
        } finally {
            // Restore original PATH
            process.env.PATH = originalPath;
        }
    }

    extractVersion(name, versionOutput) {
        switch (name) {
            case 'conda':
                return versionOutput.replace(/^conda\s+/, '');
            case 'node':
                return versionOutput.replace(/^v/, '');
            case 'python':
                return versionOutput.replace(/^Python\s+/, '');
            default:
                return versionOutput;
        }
    }
}

module.exports = { LocalSoftwareInfo };
